package math;

import sun.misc.GC;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.Arrays;

/**
 * Created by wangyg 
 * on 2017/3/29 10:39
 */
public class Test {

	public static void main(String[] args) {
		Long time0 = System.currentTimeMillis();
		int a[] = { 1, 2, 99, 67, 89, 34, 45, 23, 24, 67, 35, 26, 85, 954, 343, 3534, 565 };
		int m = 0;
		System.out.print("排序之前： ");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
		for (int i = a.length - 1; i >= 0; i--) {        //最多做n-1次排序
			int flag = 0;
			for (int j = 0; j < i; j++) {
				//					if(a[j]>a[j+1]&&a[j]>a[i]){
				//						m=a[j];
				//						a[j]=a[i];
				//						a[i]=m;
				//						flag=1;
				//					}

				if (a[j] > a[j + 1]) {
					m = a[j];
					a[j] = a[j + 1];
					a[j] = m;
					flag = 1;
				}
			}
			if (flag == 0)            //如果没有做任何的交换则直接退出循环，代表原来数组本来就排序
				break;
		}
		System.out.print("排序之后： ");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		//			System.out.println();
		//			System.out.print("DualPivotQuicksort");
		//			Arrays.sort(a);
		//
		//			System.out.println(Arrays.toString(a));
		System.out.println("总耗时：" + (System.currentTimeMillis() - time0));
	}

}
